# -*- coding = utf-8 -*-
# @Time : 2022/6/9 10:13
# @Author :Henry L
# @File : AutoCollection.py
# @Software : PyCharm
import random
from MergeExcelToCsv import fileinfo
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import logging


class Crack:
    def __init__(self):
        options = webdriver.ChromeOptions()
        service = Service(r'D:\chromedriver-win64\chromedriver.exe')
        prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'D:\\天眼查自动采集\\'}
        options.add_experimental_option('prefs', prefs)
        self.url = 'https://www.tianyancha.com/'
        self.driver = webdriver.Chrome(options=options, service=service)
        self.driver.maximize_window()
        self.wait = WebDriverWait(driver=self.driver, timeout=10)
        self.driver.get(self.url)
        logging.basicConfig(level=logging.INFO, filename='TYCAutoCollection.log',
                            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')  # 设置日志级别
        self.province = ''

    def get_login(self):
        '''
        完成登录
        :return:
        '''
        while True:
            r = float(0.1 * random.randint(20, 50))
            try:
                LoginButton = self.wait.until(EC.presence_of_element_located(
                    (By.XPATH, "/html/body/div/div[1]/div[1]/div/div[1]/div/div[2]/div/div[6]/span")
                ))
                LoginButton.click()
                time.sleep(r + 3)
                Login_Frame = self.wait.until(EC.presence_of_element_located(
                    (By.XPATH, "//div[@id='J_TYC_PC_Components_Overlay_Container_']//div[@class='login-toggle -scan']")
                ))
                Login_Frame.click()
                time.sleep(r + 3)
                self.driver.find_element(By.XPATH,"/html/body/div[@id='J_TYC_PC_Components_Overlay_Container_']"
                                                  "/div[@id='J_Modal_Container']/div/div[@class='tyc-modal-root']"
                                                  "/div[@class='tyc-modal-wrap tyc-modal-centered tyc-login-modal']"
                                                  "/div[@class='tyc-modal']/div[@class='tyc-modal-content']"
                                                  "/div[@class='tyc-modal-body']/div[@class='login-main']"
                                                  "/div[@class='sign-in sign-in-mobile']/div[@class='sign-title']"
                                                  "/div[@class='title-password']").click()
                break
            except:
                time.sleep(1.5 + r)
                self.driver.refresh()

        input = self.driver.find_elements(By.XPATH,"/html/body/div[@id='J_TYC_PC_Components_Overlay_Container_']"
                                                     "/div[@id='J_Modal_Container']//input")
        time.sleep(0.5)
        input[0].send_keys(Keys.CONTROL,"")
        time.sleep(0.5)
        input[0].send_keys("18168573683")
        time.sleep(0.5)
        input[1].send_keys('recloud918')
        self.driver.find_element(By.XPATH, "/html/body/div[@id='J_TYC_PC_Components_Overlay_Container_']"
                                            "/div[@id='J_Modal_Container']//button").click()
        time.sleep(5)

    def get_in_search(self):
        r = float(0.1 * random.randint(20, 50))
        time.sleep(1 + r + 3)
        self.driver.get('https://www.tianyancha.com/advance/search')

    def select_medical_industry(self):
        r = float(0.1 * random.randint(20, 50))
        time.sleep(r)
        self.driver.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div/div/div/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/div/div/div/div').click()
        time.sleep(0.5 + r)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[2]/div/div/div[1]/div/div/div[1]/span[6]/span/i').click()
        time.sleep(0.7 + r)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[2]/div/div/div[1]/div/div/div[2]/span[1]/span/i').click()
        time.sleep(0.3 + r)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[2]/div/div/div[1]/div/div/div[3]/span[5]/span/span[2]').click()
        time.sleep(0.1 + r)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[2]/div/div/div[1]/div/div/div[2]/span[2]/span/i').click()
        time.sleep(r - 1)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[2]/div/div/div[1]/div/div/div[3]/span[5]/span/span[2]').click()
        time.sleep(r - 1)
        self.driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[1]/div/div/div/div[2]/div[2]/div/div[2]/div[2]/div/div[1]').click()


    def auto_select_city(self):
        r = float(0.1 * random.randint(20, 50))
        time.sleep(r-1)
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div/div/div/div[2]/div[2]/div/div[2]/div[2]/div/div[2]/div/div/div/div/input').click()
        time.sleep(r)
        record = []
        for pro in range(0, 32):
            province_num = pro
            time.sleep(r-1)
            # /html/body/div[2]/div/div/div[2]/div[4]/div[2]/div/div/div[1]/div[1]/ul/li[1]
            self.driver.find_elements(By.XPATH, '/html/body/div[3]/div/div/div[1]/div/div/div[1]/span/span/span[2]')[pro].click()
            time.sleep(r-1)
            citys = self.driver.find_elements(By.XPATH, '/html/body/div[3]/div/div/div[1]/div/div/div[2]/span/span/span[2]')
            for no in range(0, len(citys)):
                city_num = no
                time.sleep(1 + r)
                self.driver.find_elements(By.XPATH, '/html/body/div[3]/div/div/div[1]/div/div/div[2]/span/span/span[2]'
                                          )[no].click()
                time.sleep(r - 1)
                self.driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[1]/div/div/div/div[2]/div[2]/div/div[2]/div[2]/div/div[1]').click()
                time.sleep(r + 1)  # 睡眠，太快的会造成100000+的情况
                num = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[3]/div/div/div/div[2]/div[1]/span').text.replace(',', '')
                if num == '100000+':
                    num = '100000'
                record.append([province_num, city_num, -1])
                if len(record) == 1 and int(num) > 50000:
                    record.clear()
                    for i in range(0, num):  # 取消第二列的选择，并且记录到record中
                        self.deselect_label_col3(pro, no, i)  # 循环上述步骤直到num< 50000为止
                        record.append([pro, no, i])
                        # 点击确定，查看num的值
                        time.sleep(r)
                        num = self.driver.find_element(By.XPATH,
                                                       '/html/body/div[2]/div/div/div[3]/div/div[1]/span').text
                        if int(num) < 50000:
                            break
                    self.export_data()  # 导出数据
                    self.resetting()  # 重置
                    for i in record:
                        self.deselect_label_col3(pro, no, i[-1])  # 重新选择

                if 42000 < int(num) < 50000:  # 如果在42000到50000之间的话会选择导出
                    self.export_data()  # 导出数据
                    self.resetting()  # 重置
                    record.clear()
                if (int(num) > 50000 and len(record) != 1) or (num == '100000+'):
                    self.deselect_label(record[-1])  # 取消选择
                    del record[0:-1]
                    self.export_data()  # 导出数据
                    self.resetting()  # 重置
                    self.deselect_label(record[-1])  # 重新选择
                    time.sleep(3)
                    num = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[3]/div/div[1]/span').text
                    if len(record) == 1 and int(num) > 50000:
                        record.clear()
                        for i in range(0, number):  # 取消第三列的选择，并且记录到record中
                            self.deselect_label_col3(pro, no, i)  # 循环上述步骤直到num< 50000为止
                            record.append([pro, no, i])
                            self.driver.find_element(By.XPATH,
                                                     '/html/body/div[2]/div/div/div[2]/div[4]/div[2]/div/div/div[2]/div/div[2]').click()  # 点击确定，查看num的值
                            time.sleep(r)
                            num = self.driver.find_element(By.XPATH,
                                                           '/html/body/div[2]/div/div/div[3]/div/div[1]/span').text
                            if int(num) < 50000:
                                break
                        self.export_data()  # 导出数据
                        self.resetting()  # 重置
                        for i in record:
                            self.deselect_label_col3(pro, no, i[-1])  # 重新选择

                while True:
                    try:
                        self.driver.find_elements(By.XPATH, '//body/div[2]/div/div/div[2]/div[4]'
                                                            '/div[2]/div/div/div[1]/div[1]/ul/li/span')[pro].click()
                        break
                    except:
                        time.sleep(r)
                        self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div[4]/div[2]/div').click()

    def deselect_label(self, record):
        '''
        对于超出5W的取消超出的选择
        :param record:
        :return:
        '''
        r = float(0.1 * random.randint(20, 50))
        time.sleep(0.7 + r)
        self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div[4]/div[2]/div/span').click() #打开地区选项
        time.sleep(r - 0.7)
        self.driver.find_element(By.XPATH, '//body/div[2]/div/div/div[2]/div[4]/div[2]/div/div/div[1]/div[1]/ul/li['+str(record[0]+1)+']/span').click()
        time.sleep(r - 1)
        city = self.driver.find_element(By.XPATH, '//body/div[2]/div/div/div[2]/div[4]'
                                               '/div[2]/div/div/div[1]/div[2]/ul/li['+ str(record[1] + 1)+']')  # 城市
        city.find_element(By.TAG_NAME, 'label').click()
        time.sleep(r - 1.5)
        self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div[4]/div[2]/div/div/div[2]/div/div[2]').click()  # confirm

    def deselect_label_col3(self, pro, no, i):
        r = float(0.1 * random.randint(20, 50))
        time.sleep(0.5 + r)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[2]/div/div/div[2]/div[4]/div[2]/div/span').click()  # 打开地区选项
        time.sleep(1.5 + r)
        self.driver.find_element(By.XPATH,
                                 '//body/div[2]/div/div/div[2]/div[4]/div[2]/div/div/div[1]/div[1]/ul/li[' + str(
                                     pro + 1) + ']/span').click()  # 选择省份
        time.sleep(1.6 + r)
        self.driver.find_element(By.XPATH,
                                 '//body/div[2]/div/div/div[2]/div[4]/div[2]/div/div/div[1]/div[2]/ul/li[' + str(
                                     no + 1) + ']/span').click()  # 选择城市
        time.sleep(1.7 + r)
        self.driver.find_element(By.XPATH,
                                 '//body/div[2]/div/div/div[2]/div[4]/div[2]/div/div/div[1]/div[3]/ul/li[' + str(
                                     i + 1) + ']/label').click()
        time.sleep(r)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[2]/div/div/div[2]/div[4]/div[2]/div/div/div[2]/div/div[2]').click()


    def export_data(self):
        '''
        导出数据，
        :param ele:
        :return:
        '''
        r = float(0.1 * random.randint(20, 50))

        # num = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[3]/div/div[1]/span').text
        # logging.info('第 %s 个预存选项的数目为 %s' % (str(50 - i), num))
        time.sleep(0.5 + r)
        time.sleep(0.5 + r)
        self.driver.find_elements(By.CLASS_NAME, 'button-primary')[2].click()  # 查看结果
        time.sleep(0.8 + r )
        self.driver.find_elements(By.CLASS_NAME, 'data_export_btn')[0].click()  # 导出数据
        time.sleep(0.4 + r)
        iframe = self.driver.find_element(By.TAG_NAME, "iframe")
        self.driver.switch_to.frame(iframe)
        self.driver.find_element(By.XPATH, '//*[@id="J_Modal_Container"]/div/div/div[2]/div/div[2]/div/div[3]/div[2]').click()  # 立即导出
        time.sleep(0.9 + r)
        self.driver.find_element(By.CLASS_NAME, "export-btn").click()
        time.sleep(0.9 + r)
        self.driver.switch_to.default_content()
        time.sleep(0.4 + r)
        self.driver.get("https://www.tianyancha.com/usercenter/report")
        time.sleep(0.5 + r)
        # self.driver.find_element(By.XPATH, "/html/body/div[2]/div/div/div[2]/div/div/div[1]/span[2]").click()
        # while True:
        #     r = 0.1 * random.randint(20, 50)
        #     # text = self.driver.find_element(By.XPATH,
        #     #             '/html/body/div[2]/div/div/div[2]/div/div/div[2]/div[2]/div/table/tbody/tr[1]/td[4]/span').text
        #     text = self.driver.find_element(By.XPATH,
        #                                     '/html/body/div[2]/div/div/div[2]/div/div/div/div/table/tbody/tr[1]/td[3]/span').text
        #     print(text)
        #     if text == '生成成功':
        #         self.driver.find_element(By.XPATH,
        #                                  '/html/body/div[2]/div/div/div[2]/div/div/div/div/table/tbody/tr[1]/td[4]/a'
        #                                  ).click()
        #         break
        #     time.sleep(5 + r)
        # # 后退两次
        while True:
            time.sleep(r - 1)
            url = self.driver.current_url
            try:
                if url == 'https://www.tianyancha.com/advance/search':
                    break
                else:
                    self.driver.back()
            except:
                self.driver.back()


    def resetting(self):
        r = float(0.1 * random.randint(20, 50))
        time.sleep(0.7 + r)
        self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div[4]/div[2]/div/span').click()
        time.sleep(0.3 + r)
        self.driver.find_element(By.XPATH,
                                 '/html/body/div[2]/div/div/div[2]/div[4]/div[2]/div/div/div[2]/div/div[1]').click()
        time.sleep(0.4 + r)
        self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div[4]/div[2]/div/div/'
                                           'div[2]/div/div[2]').click()  # confirm

if __name__ == '__main__':
    crack = Crack()
    crack.get_login()
    time.sleep(5)
    crack.get_in_search()
    # 获取已经设置好的50个选项
    crack.select_medical_industry()
    crack.auto_select_city()
    time.sleep(60)
    crack.driver.quit()
    # 需要等待最后一个文件下载完成
    # files = []
    # file_info = fileinfo()  # 获取文件的创建时间
    # files_or_not = file_info.get_FileName()
    #
    #
    # for i in files_or_not:
    #     file_path = file_info.file_dir + '\\' + i
    #     flag = file_info.pat.search(file_path)
    #     '''file_info.Compare_CreateTime(file_path) and flag:'''
    #     while flag:   # 比对文件创建时间与天眼查文件命名规范，符合
    #         files.append(file_path)
    #         break
    #
    #
    # rename = {
    #     '公司名称': 'name',
    #     '经营状态': 'account_status',
    #     '法定代表人': 'legal_person',
    #     '注册资本': 'register_amount',
    #     '实缴资本': 'real_amount',
    #     '成立日期': 'establish_date',
    #     '核准日期': 'approved_time',
    #     '营业期限': 'business_term',
    #     '所属省份': 'province',
    #     '所属城市': 'city',
    #     '所属区县': 'county',
    #     '统一社会信用代码': 'social_credit_code',
    #     '纳税人识别号': 'tax_number',
    #     '注册号': 'reg_number',
    #     '组织机构代码': 'org_number',
    #     '参保人数': 'social_staff_num',
    #     '公司类型': 'business_type',
    #     '所属行业': 'industry',
    #     '曾用名': 'account_his',
    #     '注册地址': 'address',
    #     '最新年报地址': 'new_address',
    #     '网址': 'website',
    #     '可用电话\n号码正常，可联系': 'telephone',
    #     '不可用电话\n空号、停机、沉默号、风险号，或同企业电话数量过多，建议跳过': 'wrongtel',
    #     '其他号码\n未成功检测号码，或固话，或非大陆号码': 'telephone2',
    #     '邮箱': 'emal',
    #     '其他邮箱': 'email2',
    #     '经营范围': 'business_scope'
    # }
    #
    # flags = file_info.merge_excel(files, rename)
    # if flags:
    #     # file_info.remove_file(files)
